package com.example.helloworld;

import java.util.ArrayList;
import java.util.Collection;

import com.example.helloworld.tools.Tools;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyDataAdapter extends ArrayAdapter<Item> {

//	private ArrayList<Item> items;
	private Context context;
	
	public MyDataAdapter(Context context, int viewId, ArrayList<Item> items) {
		super(context, viewId, items);
//		this.items = items;
		this.context = context;
		Tools.print("Count " + getCount());
	}

	public void setItems(ArrayList<Item> items) {
//		this.items = items;
		clear() ;
		addAll(items);
		notifyDataSetChanged();
		Tools.print("Count 2 " + getCount());
	}
	



//	@Override
//	public void add(Item object) {
//		super.add(object);
//	}
	
//	public void addItem(int position, Item item) {
//		//TODO
//		items.add(position, item);
//	}
	
//	public Item getItem(int position) {
//		ItemObject item = (ItemObject)items.get(position);
//		return item;
// 	}
//	@Override
//	public Item getItem(int position) {
//		Item item = items.get(position);
//		return item;
// 	}
	
//	public String getTitle(int position) {
//		return ((ItemObject) items.get(position)).getTitle();
//	}
//
//	public void setItemTitle(int position, String title) {
//		ItemObject item = (ItemObject)items.get(position);
//		item.setTitle(title);
//	}
//	
//	public void setItemPrice(int position, String price) {
//		ItemObject item = (ItemObject)items.get(position);
//		item.setPrice(Integer.parseInt(price));
//	}
//	
//	public void setItemStatus(int position, String status) {
//		ItemObject item = (ItemObject)items.get(position);
//		item.setStatus(status);
//	}
//	
	public void setItem(int position, Item item) {
		remove(getItem(position));
		insert(item, position);
		notifyDataSetChanged();
		// getItem(position);
		// items.set(position, item);
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Item item = getItem(position);//items.get(position);
		
		LayoutInflater inf = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (item instanceof ItemObject) {
				
			ItemObject itemObj = (ItemObject) item;
			convertView = inf.inflate(R.layout.list_item_layout, null);
			
			setItemObjectView(itemObj, convertView);
			
		} else if (item instanceof ItemSection) {
			// TODO
			ItemSection itemSect = (ItemSection) item;
			
			LinearLayout layout = new LinearLayout(context);
			
			TextView name = new TextView(context);
			
			layout.addView(name);
			
			name.setText(itemSect.getTitle());
			
			convertView = layout;
			
			convertView.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					return false;
				}
			});
		}

		return convertView;
	}
	

	private void setItemObjectView(ItemObject itemObj, View view) {
		
		ImageView image = (ImageView) view.findViewById(R.id.icon);
		TextView status = (TextView) view.findViewById(R.id.status);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView price = (TextView) view.findViewById(R.id.price);
		
		image.setBackgroundResource(itemObj.getImageId());
		status.setText(itemObj.getStatus());
		title.setText(itemObj.getTitle());
		price.setText(Integer.toString(itemObj.getPrice()));
		
	}

	public void deleteItem(int position) {
		remove(getItem(position));
//		items.remove(position);
		this.notifyDataSetChanged();
		Tools.print("Count 2 " + getCount());
	}

}
